local LuaAoi = _G.lua_aoi
local Util = require("common.Util")
local Msg = require("core.Msg")
local Obj = require("core.Obj")
local Timer = require("core.Timer")
local Skill = require("skill.Skill")
local Conditions = require("skill.Conditions")
local CombatCalc = require("skill.CombatCalc")
local EffectLogic = require("scene.EffectLogic")
local RoleCombatMode = require("role.RoleCombatMode")

list = list or {}
if not pool then
	pool = {}
	for i = 1, 8192 do
		pool[i] = {}
	end
end

function create(owner,bodyID,bottomID,skillID,skillLevel,friendAndFoeConditions,radius)
	local trap = pool[#pool]
	if trap then
		pool[#pool] = nil
	else
		return
	end
	
	Obj.create(trap, Obj.TYPE_TRAP)
	trap.owner = trap.owner or {}
	owner = owner.owner or owner
	local ownerData = trap.owner
	ownerData.id = owner.id
	
	ownerData.obj_type = owner.obj_type
	ownerData.uid = owner.uid
	CombatCalc.setAttrMount(trap,owner)		--保存主人属性
	trap.camps = owner.camps
	trap.bodyID = bodyID
	trap.bottomID = bottomID
	trap.skillID = skillID
	trap.skillLevel = skillLevel
	trap.friendAndFoeConditions = friendAndFoeConditions
	trap.radius = radius
	trap.isDie = false
	trap.sub = nil	--召唤数量上限控制
	trap.createTime = Timer.now
	
	list[trap.id] = trap
	return trap
end

function getObjAdd(trap)
	local mm = Msg.gc.GC_ADD_AOE
	local owner = Obj.getOwner(trap)
	if owner and owner.obj_type == Obj.TYPE_HUMAN then
		mm.owner_uuid = owner._id
		mm.owner_combatSide =  RoleCombatMode.getCombatSide(owner)
		mm.owner_combatMode =  RoleCombatMode.getCombatMode(owner)
	else
		mm.owner_uuid = ""
		mm.owner_combatSide = 0
		mm.owner_combatMode = 0
	end
	
	mm.speed = trap.speed or 0
	mm.obj_id = trap.id
	mm.bodyID = trap.bodyID
	mm.bottomID = trap.bottomID
	mm.bodyEffectConfig =  EffectLogic.getConfig(trap.bodyID)
	mm.bottomEffectConfig =  EffectLogic.getConfig(trap.bottomID)
	mm.x = trap.x
	mm.y = trap.y
	mm.comboType = 0
	mm.comboArg1 = 0
	mm.comboArg2 = 0
	mm.isDie = trap.isDie and 1 or 0
	mm.show = trap.createTime == Timer.now and 0 or 1
	mm.dir = trap.dir or 0
	mm.onlyShowOwner = 1
	local len = trap.path_len or 0
	mm.points[0] = len * 2
	for i = 1, len do
		local j = len + 1 - i
		mm.points[2 * i - 1] = trap.path[2 * j - 1]
		mm.points[2 * i] = trap.path[2 * j]
	end
	return mm
end

local empty = {}
function getBuf()
	return empty, 0
end
function setBufLen()
end

function getName()
	return "trap_name"
end

function destroy(trap)
	if not list[trap.id] then
		assert()
	end
	pool[#pool + 1] = trap
	trap.die = 1
	list[trap.id] = nil
	Obj.destroy(trap)
end

function onTimer()
	for id,trap in pairs(list) do
	
		local owner = Obj.getOwner(trap)
		if not owner or owner.uid~=trap.owner.uid or (trap.dieTime and Timer.now>=trap.dieTime) then
			destroy(trap)
		elseif not trap.isDie and Timer.now-trap.createTime>=500 then
			
			local warnRadius = trap.radius*2
			for _, obj in pairs(Obj.scenes[trap.scene_id]) do
				if not obj.hide and obj ~= trap then
					local dx = obj.x - trap.x
					local dy = obj.y - trap.y
					if -warnRadius < dx and dx < warnRadius and -warnRadius < dy and dy < warnRadius then						
						if Conditions.canAttack(obj) and Conditions.friendAndFoeConditions(owner,obj,trap.friendAndFoeConditions) then
							local xNew, yNew = LuaAoi.pos_fix(obj, Timer.last_exe_time.walk, Timer.now)
							if Util.roundCollision(trap.x,trap.y,trap.radius,xNew,yNew,Obj.getRadius(obj)) then	-- 椭圆碰撞
								trap.isDie = true
								Obj.sendGCObjDie(trap)
								if 0 < trap.skillID then
									Skill.add(trap,trap.skillID,trap.x,trap.y,trap.x,trap.y,nil,trap.skillLevel)
								end
								break
							end
						end
					end
				end
			end
		end
	end	
end

function onSkill(Obj)
  return Obj
end